package com.example.othree.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.othree.entity.PollutionHistoryDataJjjSitesMonth;
import com.example.othree.mapper.PollutionHistoryDataJjjSitesMonthMapper;
import com.example.othree.service.PollutionHistoryDataJjjSitesMonthService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PollutionHistoryDataJjjSitesMonthServiceImpl extends ServiceImpl<PollutionHistoryDataJjjSitesMonthMapper, PollutionHistoryDataJjjSitesMonth> implements PollutionHistoryDataJjjSitesMonthService{

    @Resource
    PollutionHistoryDataJjjSitesMonthMapper pollutionHistoryDataJjjSitesMonthMapper;


    @Override
    public List<PollutionHistoryDataJjjSitesMonth> pollutionAnalyzeDataByDate(String startTime, String endTime, String month, String[] cities) {

        String monthInfo = null;
        monthInfo = "%-" + convertMonth(month);

        return pollutionHistoryDataJjjSitesMonthMapper.pollutionForSite(Integer.parseInt(startTime), Integer.parseInt(endTime), monthInfo, cities);
    }

    @Override
    public ArrayList<Float> pollutionAnalyzeDataByCityForAvg(String startTime, String endTime, String element, String[] cities) {
        return pollutionHistoryDataJjjSitesMonthMapper.pollutionForSiteByAvea(startTime, endTime, element, cities);
    }

    @Override
    public List<HashMap<String, String>> pollutionAnalyzeDataByCityForEvery(String startTime, String endTime, String element, String[] cities) {
        return pollutionHistoryDataJjjSitesMonthMapper.pollutionForSiteByEvery(startTime, endTime, element, cities);
    }

    @Override
    public PollutionHistoryDataJjjSitesMonth pollutionForLevel(String startTime, String endTime, String region, String month) {
        List<PollutionHistoryDataJjjSitesMonth> p = pollutionHistoryDataJjjSitesMonthMapper.pollutionForLevel(startTime, endTime, new String[]{region});
        for (PollutionHistoryDataJjjSitesMonth i : p){
            if (i.getMonth().equals(convertMonth(month))) return i;
        }
        return null;
    }

    @Override
    public PollutionHistoryDataJjjSitesMonth pollutionForCount(String startTime, String endTime, String region, String month) {
        List<PollutionHistoryDataJjjSitesMonth> p = pollutionHistoryDataJjjSitesMonthMapper.pollutionForCount(startTime, endTime, new String[]{region});
        for (PollutionHistoryDataJjjSitesMonth i : p){
            if (i.getMonth().equals(convertMonth(month))) return i;
        }
        return null;
    }


    public static String convertMonth(String month) {
        // 匹配“1月”，“11月”，“12月”
        String regex = "(\\d+)(月)";
        String replacement = "$1";

        // 如果匹配结果长度小于2，前面补0
        String result = month.replaceAll(regex, replacement);
        return result.length() == 1 ? "0" + result : result;
    }
}
